package com.example.graduatedesigned.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.graduatedesigned.R;
import com.example.graduatedesigned.activity.PredictActivity;
import com.example.graduatedesigned.adapter.HistoryAdapter;
import com.example.graduatedesigned.bean.DataEvent;
import org.greenrobot.eventbus.EventBus;
import com.example.graduatedesigned.bean.DataResponse;
import com.example.graduatedesigned.bean.DeviceHistoryItem;
import com.example.graduatedesigned.bean.HistoryBean;
import com.example.graduatedesigned.http.ApiService;
import com.example.graduatedesigned.http.RetrofitClient;
import com.example.graduatedesigned.utils.DevicesInfo;
import com.google.android.material.button.MaterialButton;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HistoryFragment extends Fragment {
    private String mParam1;
    private static final String ARG_PARAM1 = "param1";
    private HistoryAdapter adapter;
    public static HistoryFragment newInstance(String param1) {
        HistoryFragment fragment = new HistoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            ApiService service = RetrofitClient.getInstance();
            String productId = DevicesInfo.product_id;
            String deviceName = DevicesInfo.device_name;
            String auto = DevicesInfo.auth_info;
            long endTime = System.currentTimeMillis();
            long startTime = endTime - 7 * 24 * 60 * 60 * 1000;
            Call<DataResponse<HistoryBean>> call = service.getDeviceHistory(productId, deviceName, auto, startTime, endTime, mParam1);
            call.enqueue(new Callback<DataResponse<HistoryBean>>() {
                @Override
                public void onResponse(Call<DataResponse<HistoryBean>> call, Response<DataResponse<HistoryBean>> response) {
                    if (response.body() == null) {
                        return;
                    }
                    List<DeviceHistoryItem> hisList = response.body().getData().getList();
                    if (hisList.isEmpty()) return;
                    adapter.setNewInstance(hisList);
                    adapter.notifyDataSetChanged();
                    EventBus.getDefault().postSticky(new DataEvent(hisList, mParam1));
                }

                @Override
                public void onFailure(Call<DataResponse<HistoryBean>> call, Throwable t) {
                }
            });
        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_history, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView rv = view.findViewById(R.id.history_rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new HistoryAdapter(R.layout.history_item, mParam1);
        rv.setAdapter(adapter);
        MaterialButton button = view.findViewById(R.id.go2Predict);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), PredictActivity.class);
                startActivity(intent);
            }
        });
    }
}
